package com.delamer.facile.exe;

import com.delamer.facile.db.DBUtilHome;
import com.delamer.facile.obj.ObjectHome;
import com.delamer.utils.MsgUtil;
import com.delamer.utils.PermitUtil;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ExecutorHome {
    public void help(CommandSender sender, String[] args) throws Exception {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(Bukkit.getPluginManager().getPlugin("DelamerFacile").getResource("home.help"), "UTF-8"));
        List<String> results = new ArrayList<String>();
        String line = null;
        while ((line = bufferedReader.readLine()) != null)
            results.add(line);
        sender.sendMessage(results.toArray(new String[]{}));
    }

    public void list(CommandSender sender, String[] args) throws Exception {
        if (!(sender instanceof Player)) {
            sender.sendMessage(MsgUtil.err("该指令无法通过控制台使用"));
            return;
        }
        if(!PermitUtil.hasPermit(sender.getName(),"home")){
            sender.sendMessage(MsgUtil.err("未取得授权的玩家无法使用该指令"));
            return;
        }
        String msg = "§e家列表：§f";
        List<ObjectHome> list = DBUtilHome.getHomeList(sender.getName());
        if (list.size() == 0) {
            msg = msg + "无";
        } else {
            for (ObjectHome object : list) msg = msg + object.getHomeName() + ", ";
            msg = msg.substring(0, msg.length() - 2);
        }
        sender.sendMessage(msg);
    }

    public void info(CommandSender sender, String[] args) throws Exception {
        if (!(sender instanceof Player)) {
            sender.sendMessage(MsgUtil.err("该指令无法通过控制台使用"));
            return;
        }
        if(!PermitUtil.hasPermit(sender.getName(),"home")){
            sender.sendMessage(MsgUtil.err("未取得授权的玩家无法使用该指令"));
            return;
        }
        if (args.length < 2) {
            sender.sendMessage(MsgUtil.err("请输入家名称"));
            return;
        }
        ObjectHome object = DBUtilHome.getHome(sender.getName(),args[1]);
        if (object == null) {
            sender.sendMessage(MsgUtil.err("家不存在"));
            return;
        }
        String msg = "§e家信息：§f";
        msg = msg.concat("\n - §b名称：§f" + object.getHomeName());
        msg = msg.concat("\n - §b世界：§f" + object.getWorldName());
        msg = msg.concat("\n - §bX：§f" + object.getLocationX());
        msg = msg.concat("\n - §bY：§f" + object.getLocationY());
        msg = msg.concat("\n - §bZ：§f" + object.getLocationZ());
        sender.sendMessage(msg);
    }

    public void add(CommandSender sender, String[] args) throws Exception {
        if (!(sender instanceof Player)) {
            sender.sendMessage(MsgUtil.err("该指令无法通过控制台使用"));
            return;
        }
        if(!PermitUtil.hasPermit(sender.getName(),"home")){
            sender.sendMessage(MsgUtil.err("未取得授权的玩家无法使用该指令"));
            return;
        }
        if (args.length < 2) {
            sender.sendMessage(MsgUtil.err("请输入家名称"));
            return;
        }
        try {
            if (DBUtilHome.getHome(sender.getName(),args[1]) != null) {
                sender.sendMessage(MsgUtil.err("家已存在"));
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
            sender.sendMessage(MsgUtil.err("添加家失败->检查家是否存在时发生错误"));
            return;
        }
        try {
            Location location = ((Player) sender).getLocation();
            ObjectHome object = new ObjectHome() {{
                setUserName(sender.getName());
                setHomeName(args[1]);
                setWorldName(location.getWorld().getName());
                setLocationX(String.valueOf((int) location.getX()));
                setLocationY(String.valueOf((int) location.getY()));
                setLocationZ(String.valueOf((int) location.getZ()));
            }};
            boolean isAdd = DBUtilHome.addHome(object);
            if (isAdd) {
                sender.sendMessage(MsgUtil.msg("添加家成功：§b" + args[1]));
            } else {
                sender.sendMessage(MsgUtil.err("添加家失败->未能添加家到数据库"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            sender.sendMessage(MsgUtil.err("添加家失败->添加家时发生错误"));
        }
    }

    public void set(CommandSender sender, String[] args) throws Exception {
        if (!(sender instanceof Player)) {
            sender.sendMessage(MsgUtil.err("该指令无法通过控制台使用"));
            return;
        }
        if(!PermitUtil.hasPermit(sender.getName(),"home")){
            sender.sendMessage(MsgUtil.err("未取得授权的玩家无法使用该指令"));
            return;
        }
        if (args.length < 2) {
            sender.sendMessage(MsgUtil.err("请输入家名称"));
            return;
        }
        try {
            if (DBUtilHome.getHome(sender.getName(),args[1]) == null) {
                sender.sendMessage(MsgUtil.err("家不存在"));
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
            sender.sendMessage(MsgUtil.err("更新家失败->检查家是否存在时发生错误"));
            return;
        }
        try {
            Location location = ((Player) sender).getLocation();
            ObjectHome object = new ObjectHome() {{
                setUserName(sender.getName());
                setHomeName(args[1]);
                setWorldName(location.getWorld().getName());
                setLocationX(String.valueOf((int) location.getX()));
                setLocationY(String.valueOf((int) location.getY()));
                setLocationZ(String.valueOf((int) location.getZ()));
            }};
            boolean isSet = DBUtilHome.setHome(object);
            if (isSet) {
                sender.sendMessage(MsgUtil.msg("更新家成功：§b" + args[1]));
            } else {
                sender.sendMessage(MsgUtil.err("更新家失败->未能更新数据库的家"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            sender.sendMessage(MsgUtil.err("更新家失败->更新家时发生错误"));
        }
    }

    public void del(CommandSender sender, String[] args) throws Exception {
        if (!(sender instanceof Player)) {
            sender.sendMessage(MsgUtil.err("该指令无法通过控制台使用"));
            return;
        }
        if(!PermitUtil.hasPermit(sender.getName(),"home")){
            sender.sendMessage(MsgUtil.err("未取得授权的玩家无法使用该指令"));
            return;
        }
        if (args.length < 2) {
            sender.sendMessage(MsgUtil.err("请输入家名称"));
            return;
        }
        try {
            if (DBUtilHome.getHome(sender.getName(),args[1]) == null) {
                sender.sendMessage(MsgUtil.err("家不存在"));
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
            sender.sendMessage(MsgUtil.err("删除家失败->检查家是否存在时发生错误"));
            return;
        }
        try {
            ObjectHome object = new ObjectHome() {{
                setUserName(sender.getName());
                setHomeName(args[1]);
            }};
            boolean isDel = DBUtilHome.delHome(object);
            if (isDel) {
                sender.sendMessage(MsgUtil.msg("删除家成功：§b" + args[1]));
            } else {
                sender.sendMessage(MsgUtil.err("删除家失败->未能从数据库删除家"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            sender.sendMessage(MsgUtil.err("删除家失败->删除家时发生错误"));
        }
    }

    public void tp(CommandSender sender, String[] args) throws Exception {
        if (!(sender instanceof Player)) {
            sender.sendMessage(MsgUtil.err("该指令无法通过控制台使用"));
            return;
        }
        if(!PermitUtil.hasPermit(sender.getName(),"home")){
            sender.sendMessage(MsgUtil.err("未取得授权的玩家无法使用该指令"));
            return;
        }
        if (args.length < 2) {
            sender.sendMessage(MsgUtil.err("请输入家名称"));
            return;
        }
        ObjectHome object = DBUtilHome.getHome(sender.getName(),args[1]);
        if (object == null) {
            sender.sendMessage(MsgUtil.err("家不存在"));
            return;
        }
        try {
            Location location = new Location(Bukkit.getWorld(object.getWorldName()), Double.valueOf(object.getLocationX()), Double.valueOf(object.getLocationY()), Double.valueOf(object.getLocationZ()));
            ((Player) sender).teleport(location);
            sender.sendMessage(MsgUtil.msg("家传送成功：§6" + object.getHomeName()));
        } catch (Exception e) {
            sender.sendMessage(MsgUtil.err("家传送失败"));
        }
    }
}
